package com.etc.security.controller;

import com.etc.security.config.JwtTokenUtils;
import com.etc.security.exception.TokenIsExpiredException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.etc.common.Code;
import com.etc.common.CodeMessage;
import com.etc.security.po.AdminMst;

@RestController
@Api(tags = "ユーザモードadmin/info")
public class AdminInfoController {

    @ApiOperation(value = "ユーザインフォを取得", notes = "token利用して、ユーザインフォを取得する")
    @ApiImplicitParam(paramType = "header", name = "Authorization", value = "ユーザ識別子", required = true, dataType = "string", example = "Bearer-eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTk5ODY5NzM5MCIsImlzcyI6InJjY3AiLCJleHAiOjE2MTI0ODg5ODgsImlhdCI6MTYxMTg4NDE4OCwicm9sIjoiUk9MRV9BRE1JTiJ9.GllT_I5MHV0R7GQ0LZmtYM-ihpemn9OYCEkBeFa6JU3HXxA40PB6d2H1bKT7PZMidL3pi11wPdMaMGK0pqKe0Q")
    @GetMapping("admin/info")
    public CodeMessage admininfo(String token) throws TokenIsExpiredException {
        AdminMst u = new AdminMst();
        if (StringUtils.isEmpty(token)) {
            return CodeMessage.fail("token なし");
        }
        String token1 = token.replace(JwtTokenUtils.TOKEN_PREFIX, "");
        boolean expiration = JwtTokenUtils.isExpiration(token1);
        if (expiration) {
            throw new TokenIsExpiredException("token超时了");
        } else {
            u.setUsername(JwtTokenUtils.getUsername(token1));
            u.setRoles(JwtTokenUtils.getUserRole(token1));
            u.setAvatar("TODO");
            u.setIntroduction("TODO");
        }
        return CodeMessage.success(Code.SUCESS, "OK",  u);
    }
}
